pepperstad.Editor = function () {

    this.game = new pepperstad.Game();
    this.currentStructure = null;

    var map, gui, loader, structureService,
        config = pepperstad.config,
        self = this;

    this.init = function () {
        this.game.init(this);
        loader = this.game.resourceLoader;
        log("editor waiting for resources to load ...");
        daemon("editorLoader", 100, function () {
            if (loader && loader.loadingComplete()) {
                log("initing editor");
                structureService = self.game.structureService;
                map = self.game.map;
                gui = self.game.gui;
                self.initButtons();
                return false;
            }
            return true;
        });
    };

    this.initButtons = function () {

        gui.createButton({
            id: "addStructureButton",
            text: "[A]dd",
            keyCode: 65,
            clickHandler: function addStructureHandler() {
                map.mode = "drawStructurePoly";
                $("#playground").css("cursor", "crosshair");
                gui.setAllButtonsActive(false);
                gui.setButtonActive("saveStructureButton", true);
                self.currentStructure = new pepperstad.Structure({
                    map: map,
                    resourceLoader: loader
                });
            }
        });
        gui.createButton({
            id: "saveStructureButton",
            text: "[S]ave",
            keyCode: 83,
            enabled: false,
            clickHandler: function saveStructureHandler() {
                var struct = self.currentStructure;
                gui.showStructureEditor(
                    null, self.saveOrEditStructureHandler(struct, true));
            }
        });
        gui.createButton({
            id: "editStructureButton",
            text: "[E]dit",
            keyCode: 69,
            clickHandler: function editStructureHandler() {
                map.mode = "editStructure";
                gui.setAllButtonsActive(false);
            }
        });
        gui.createButton({
            id: "deleteStructureButton",
            text: "[D]elete",
            keyCode: 68,
            clickHandler: function deleteStructureHandler() {
                map.mode = "deleteStructure";
                gui.setAllButtonsActive(false);
            }
        });
        gui.createSpacer();
        gui.createButton({
            id: "exportStructuresButton",
            text: "Export Structures",
            smallText: true,
            clickHandler: function exportStructuresHandler() {
                structureService.exportStructures();
            }
        });
        gui.createSpacer();
        gui.createButton({
            id: "gotoGameButton",
            text: "Play Game",
            smallText: true,
            clickHandler: function gotoGameHandler() {
                window.location.href = config.contextRoot;
            }
        });
    };

    this.saveOrEditStructureHandler = function (struct, isNew) {
        return function (sType, sName, sState) {
            struct.type = sType;
            struct.name = sName;
            struct.state = sState;
            if (isNew) {
                structureService.addStructure(struct);
            }
            // (re)draw structure outline
            struct.drawStructure(structureService.paper);
            map.mode = "pan";
            $("#playground").css("cursor", "default");
            gui.setAllButtonsActive(true);
            gui.setButtonActive("saveStructureButton", false);
        };
    };
};
